﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Text;

using CRM.Entity;
using CRM.BLL;
using DP.Web.UI;
using DP.Common;

namespace CRM.Web.SurveyManage
{
    public partial class ItemsModify : Common.PageBase
    {
        T_SURVEY_ITEMS_BLL itemsBll = new T_SURVEY_ITEMS_BLL();
        T_SURVEY_OPTIONS_BLL optionsBll = new T_SURVEY_OPTIONS_BLL();
        protected override void OnInit(EventArgs e)
        {
            base.OnInit(e);
            PageHelper.SetPrePageUrl(this.Page);
            PageHelper.SetGoBackUrl(this.Page, btnCancel, StaticConstant.NotBackUrl);
        }
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                EnumHelper.FillListControl(ddlITEM_TYPE, typeof(ItemType));
                pageDataInit();
            }
        }

        #region 页面初始化数据绑定
        #endregion

        private void pageDataInit()
        {
            string _autoid = String.IsNullOrEmpty(Request.QueryString["AUTOID"]) ? "" : Request.QueryString["AUTOID"].Trim();
            if (!String.IsNullOrEmpty(_autoid))
            {
                T_SURVEY_ITEMS_Entity entity = itemsBll.GetEntity("AUTOID", _autoid);
                PageHelper.SetControlValue<T_SURVEY_ITEMS_Entity>(this.Page, entity);
                lblTitle.Text = "题目信息修改";
            }
            else
            {
                txtAUTOID.Text = "";
                txtITEM_NAME.Text = "";
                ddlITEM_TYPE.SelectedValue = ((int)ItemType.单选).ToString();
                rbENINPUT0.Checked = true;
                lblTitle.Text = "题目信息添加";
                this.btnSETITEM_OPTIONS.Visible = false;
            }
        }

        protected void btnOK_Click(object sender, EventArgs e)
        {
            T_SURVEY_ITEMS_Entity entity = new T_SURVEY_ITEMS_Entity();
            if (txtITEM_NAME.Text == "")
            {
                PageHelper.ShowMessage(this.Page, "题目名称不能为空！");
                return;
            }
            entity = itemsBll.GetEntity("AUTOID", txtAUTOID.Text);
            if (entity == null)
            {
                entity = itemsBll.GetEntity("ITEM_NAME", txtITEM_NAME.Text);
                if (entity == null)
                {
                    entity = new T_SURVEY_ITEMS_Entity();
                    PageHelper.GetControlValue<T_SURVEY_ITEMS_Entity>(this.Page, ref entity);
                    entity.ITEM_TYPE = StringHelper.ToInt(ddlITEM_TYPE.SelectedValue);
                    entity.BUSINESSDIVISIONCODE = _userLoginInfo.UserEntity.BUSINESSDIVISIONCODE;
                    entity.BUSINESSDIVISIONID = _userLoginInfo.UserEntity.BUSINESSDIVISIONID;
                    entity.BUSINESSDIVISIONNAME = _userLoginInfo.UserEntity.BUSINESSDIVISIONNAME;
                    itemsBll.Insert(entity);
                }
                else
                {
                    PageHelper.ShowMessage(this.Page, "题目已经存在，请核实后再进行增加");
                    return;
                }
            }
            else
            {
                PageHelper.GetControlValue<T_SURVEY_ITEMS_Entity>(this.Page, ref entity);
                entity.ITEM_TYPE = StringHelper.ToInt(ddlITEM_TYPE.SelectedValue);
                itemsBll.Update(entity);
            }
            PageHelper.ShowMessage(this.Page, "保存成功！","ItemsList.aspx");
        }
        /// <summary>
        /// 设定题目选项
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void btnSETITEM_OPTIONS_Click(object sender, EventArgs e)
        {
            if (string.IsNullOrEmpty(txtAUTOID.Text))
            {
                PageHelper.ShowMessage(this.Page, "题目编号不能为空，请选择需要操作的题目！");
                return;
            }
            if (String.IsNullOrEmpty(ddlITEM_TYPE.SelectedValue) || ddlITEM_TYPE.SelectedValue == ((int)ItemType.问答).ToString())
            {
                PageHelper.ShowMessage(this.Page, "题目类型为空或问答，不能做设定题目选项操作！");
                return;
            }
            T_SURVEY_ITEMS_Entity entity = new T_SURVEY_ITEMS_Entity();
            entity = itemsBll.GetEntity("AUTOID", txtAUTOID.Text);
            if(entity==null)
            {
                PageHelper.ShowMessage(this.Page, "请先保存题目，再做设定题目选项操作！");
                return;
            }
            Response.Redirect(String.Format("ItemsOptionsList.aspx?ITEM_ID={0}", txtAUTOID.Text));
        }
    }
}
